package cn.com.gridinfo.sooc.core.expand.score.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.score.bean.Counts;

/**
 * 我的积分Adapter
 *
 * @author yingchunli
 */
public class MyCountsAdapter extends UltimateViewAdapter<MyCountsAdapter.ViewHolder> {

    public List<Counts> countsInfoList;
    Context context;

    public MyCountsAdapter(List<Counts> countsInfoList, Context context) {
        this.countsInfoList = countsInfoList;
        this.context = context;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_score_item, parent, false);

        return new ViewHolder(view, true);
    }


    @Override
    public int getAdapterItemCount() {
        return countsInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= countsInfoList.size() : position < countsInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            Counts counts = countsInfoList.get(position);
            holder.time.setText(counts.getTime());//设置时间
            holder.type.setText(counts.getType());//设置类型
            //积分明细  显示类型  不显示 成功与失败
            holder.type.setVisibility(View.VISIBLE);
            //设置金额  和金额的颜色
            holder.count.setText(counts.getCount());
            if (Integer.parseInt(holder.count.getText().toString().trim()) > 0) {
                holder.count.setTextColor(context.getResources().getColor(R.color.red_hint));//设置为红色字
            } else {
                holder.count.setTextColor(context.getResources().getColor(R.color.green_score));//设置为绿色字
            }

            //底部分割线的显示
//            if (position==countsInfoList.size()-1){
//                holder.line.setVisibility(View.VISIBLE);
//            }else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    public void loadmore(List<Counts> list) {
        countsInfoList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<Counts> list) {
        countsInfoList.clear();
        countsInfoList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView time, type, count;
        View line;//底部分割线

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                time = (TextView) itemView.findViewById(R.id.time_score);//时间
                type = (TextView) itemView.findViewById(R.id.type_score);//类型 只用于积分明细
                count = (TextView) itemView.findViewById(R.id.count_score);//金额
//                line = itemView.findViewById(R.id.line_score);
            }
        }
    }
}
